package com.hgl.bi.web.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgl.bi.common.constant.ErrorCode;
import com.hgl.bi.common.model.dto.PageDTO;
import com.hgl.bi.common.utils.AssertUtil;
import com.hgl.bi.web.entity.SetEntity;
import com.hgl.bi.web.mapper.MetadataMapper;
import com.hgl.bi.web.mapper.SetMapper;
import com.hgl.bi.web.model.dto.SharedSetDto;
import com.hgl.bi.web.model.vo.FileVo;
import com.hgl.bi.web.service.SetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: BI
 * @description: 数据集管理服务实现
 * @author: hgl
 * @create: 2025-02-16 21:34
 */
@Service
public class SetServiceImpl extends ServiceImpl<SetMapper, SetEntity>
        implements SetService {

    @Autowired
    private MetadataMapper metadataMapper;

    @Autowired
    private SetMapper setMapper;

    @Override
    public List<FileVo> getAssociatedFiles(Long id) {
        List<FileVo> list = metadataMapper.selectAssociatedFiles(id);
        return list;
    }

    @Override
    public void sharedSet(long userId, SharedSetDto sharedSetDto) {
        Long setId = sharedSetDto.getSetId();
        List<Long> userIds = sharedSetDto.getUserIds();

        // 非空校验
        AssertUtil.notNull(userId, ErrorCode.USER_ID_NULL);
        AssertUtil.notNull(setId, ErrorCode.SET_ID_NULL);
        AssertUtil.notNull(userIds, ErrorCode.USER_IDS_NULL);
        // 分享数据集
        boolean shared = setMapper.sharedSet(userId, setId, userIds);
        AssertUtil.isTrue(shared, ErrorCode.SHARED_SET_FALSE);

    }

    @Override
    public void removeSharedSet(long userId, SharedSetDto sharedSetDto) {
        Long setId = sharedSetDto.getSetId();
        List<Long> userIds = sharedSetDto.getUserIds();

        // 非空校验
        AssertUtil.notNull(userId, ErrorCode.USER_ID_NULL);
        AssertUtil.notNull(setId, ErrorCode.SET_ID_NULL);
        AssertUtil.notNull(userIds, ErrorCode.USER_IDS_NULL);

        // 取消分享数据集
        boolean removed = setMapper.removeSharedSet(userId, setId, userIds);
        AssertUtil.isTrue(removed, ErrorCode.CANCEL_SHARED_SET_FALSE);
    }

    @Override
    public Page<SetEntity> sharedPage(long userId, PageDTO pageDto) {
        Integer page = pageDto.getPage();
        Integer size = pageDto.getSize();
        String keyword = pageDto.getKeyword();
        Integer offset = (page - 1) * size;

        List<SetEntity> list = setMapper.sharedPage(userId, offset, size, keyword);
        Page<SetEntity> setEntityPage = new Page<>();
        setEntityPage.setRecords(list);
        Long total = setMapper.getTotalSharedSets(userId, keyword);
        setEntityPage.setTotal(total == null ? 0 : total);
        setEntityPage.setSize(size);
        setEntityPage.setCurrent(page);
        setEntityPage.setPages(total == null ? 0 : (int) Math.ceil((double) total / size));
        return setEntityPage;
    }

    @Override
    public long countSharedSets(long userId) {
        Long total = setMapper.selectSharedSets(userId);
        return total;
    }

    @Override
    public List<Long> getSharedUsers(long userId, Long id) {
        List<Long> list = setMapper.selectSharedUsers(userId, id);
        return list;
    }
}
